package com.fdb.smcts.core.util;

import org.springframework.stereotype.Service;

@Service
public class Des3Utils {


    /**
     * dec:(加密).
     * @param key 密钥 十六进制字符串
     * @param content  明文字符串
     * @return 返回结果：String 十六进制字符串
     */
    public static String encryption(String key, String content) {
        Des3EncryptUtils des = new Des3EncryptUtils();
        String enKey = "";//最终加密秘钥48位
        if(key.length() <= 32){
            enKey = (key + key).substring(0, 48);
        }else if(key.length() >= 48){
            enKey = key.substring(0, 48);
        }

        des.setKey(HexUtils.hex2Byte(enKey));
        String hexStr= HexUtils.str2HexStr(content);
        if(hexStr.length() <= 16){
            hexStr = (hexStr+"0000000000000000").substring(0,16);
        }

        if(hexStr.length() >= 16){
            hexStr = hexStr.substring(0, 16);
        }
        byte[] bye = des.get3DesEncCode(HexUtils.hex2Byte(hexStr));
        return HexUtils.byte2Hex(bye).trim();
    }
}
